<template>
  <div class="portal-detail-container"
       v-loading="loading"
       element-loading-text="数据加载中, 请稍后..."
       element-loading-spinner="el-icon-loading"
       element-loading-background="rgba(0, 0, 0, 0.1)">

    <div class="query-search-thin" style="margin-bottom: 10px;">
      <el-form :inline="true" size="mini"></el-form>
      <el-button type="primary" @click="handleCreate()" size="mini" icon="el-icon-plus">生成报表数据</el-button>
    </div>

    <div class="table-wrapper">
      <el-table
        :data="list"
        border
        size="mini"
        style="width: 100%">
        <el-table-column
          prop="title"
          label="报表名">
          <template #default="scope">
            {{ scope.row.report_define.title }}
          </template>
        </el-table-column>
        <el-table-column
          prop="create_time"
          label="创建时间"
          align="center">
        </el-table-column>

        <el-table-column label="操作" width="80" align="center">
          <template #default="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="refresh"
      @current-change="refresh"
      :current-page.sync="pagination.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size.sync="pagination.pageSize"
      layout="total, sizes, prev, pager, next"
      :total="pagination.total">
    </el-pagination>
  </div>
</template>

<script setup>
import { ref, watch, onMounted } from 'vue';
import _ from 'lodash';
import {api} from "@jzl/api";

const props = defineProps({
  reportDefineId: Number
});


const loading = ref(false);
const pagination = ref({
  page: 1,
  pageSize: 10,
  total: 0
});
const list = ref([]);

const init = () => {
  pagination.value.page = 1;
  refresh();
};

const refresh = async () => {
  loading.value = true;
  let ret = await api.report.getReportDatas(props.reportDefineId, pagination.value);
  list.value = _.get(ret, 'rows', []);
  pagination.value.total = _.get(ret, 'count', 0);
  loading.value = false;
};

const _delete = async (id) => {
  let ret = await api.report.removeReportData(id);

  if (ret) {
    refresh();
  }
};

const handleDelete = (it) => {
  if (!it || !it.reportdata_id) {
    return;
  }

  let title = _.get(it, 'report_define.title', '');
  $jui.confirm(`此操作将永久删除报表此条数据【${title}】, 是否继续?`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await _delete(it.reportdata_id);
  }).catch(() => {
    // Handle cancel
  });
};

const handleCreate = async () => {
  let ret = await api.report.createReportData(props.reportDefineId);

  if (ret && ret.reportdata_id) {
    $jui.notify({
      title: '提示',
      message: '操作成功！',
      dangerouslyUseHTMLString: true,
      type: 'warning'
    });
  }

  refresh();
};

onMounted(() => {
  init();
});

watch(() => props.reportDefineId, () => {
  init();
});
</script>
